<template>
	<view class="page">
		<template v-if="isLogin">
			<view class="body">
				<view class="form-wrap m-b2">
					<!-- <view class="form-item">
						<view class="label">姓 名：</view>
						<view class="form-input">
							<view class="content">
								<input type="text" class="inp f-grey" v-model="userInfo.realname" placeholder="" disabled />
							</view>
						</view>
					</view>
					<view class="form-item">
						<view class="label">手机号：</view>
						<view class="form-input">
							<view class="content">
								<input type="text" class="inp f-grey" v-model="replace_phone" placeholder="" disabled />
							</view>
						</view>
					</view> -->
					<view class="form-item">
						<view class="label">省 份：</view>
						<picker mode="selector" :range="provinces" range-key="name" class="form-picker" @change="changeProvince">
							<view class="picker">
								<input type="text" v-model="params.province.name" disabled class="select-data t-l" placeholder="请选择省份">
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>
					<view v-if="params.province.name" class="form-item">
						<view class="label">城 市：</view>
						<picker mode="selector" :range="citys" range-key="name" class="form-picker" @change="changeCity">
							<view class="picker">
								<input type="text" v-model="params.city.name" disabled class="select-data t-l" placeholder="请选择城市">
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>
					<view v-if="params.city.name" class="form-item">
						<view class="label">区/县：</view>
						<picker mode="selector" :range="districts" range-key="name" class="form-picker" @change="changeDistrict">
							<view class="picker">
								<input type="text" v-model="params.district.name" disabled class="select-data t-l" placeholder="请选择区/县">
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>
					<view v-if="params.district.name" class="form-item">
						<view class="label">镇/街道：</view>
						<picker class="form-picker" :range="towns" range-key="name" mode="selector" @change="changeTown">
							<view class="picker">
								<input type="text" v-model="params.town.name" disabled class="select-data t-l" placeholder="请选择镇/街道">
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>
					<view v-if="params.town.name" class="form-item">
						<view class="label">村/社区：</view>
						<picker class="form-picker" :range="villages" range-key="name" mode="selector" @change="changeVillage">
							<view class="picker">
								<input type="text" v-model="params.village.name" disabled class="select-data t-l" placeholder="选择村/社区">
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>
					 
				</view>
			</view>
			<view class="footer">
				<view class="fix-wrap bottom">
					<view class="form-lay-btn" data-id="areaSearchinfo" @click="saveArea">保存</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	import {replaceStr} from '@/common/util.js'
 	export default {
		name: 'demo',
		data() {
			return {
				isLogin: false,
				userInfo: uni.getStorageSync('userInfo'),
				provinces:[],
				citys:[],
				districts:[],
				towns:[],
				villages:[],
				name: '',
				phone:'',
				replace_phone:'',
				params: {				
					province: {
						name:'',
						adcode:'',
						level:''
					},
					city: {
						name:'',
						adcode:'',
						level:''
					},
					district: {
						name:'',
						adcode:'',
						level:''
					},
					town: {
						name:'',
						adcode:'',
						level:''
					},
					village: {
						name:'',
						adcode:'',
						level:''
					} 
				}, 
				detail:{}
			}
		},
		onShow() {

		},
		async onLoad() { 
			this.isLogin = await this.$loginState('check'); 
			if(this.isLogin){ 
				this.replace_phone = replaceStr(this.userInfo.phone, 3, '****');
				this.provinces=await this.getArea('china','china');  
				this.params=await this.getUserAddressInfo();  
				if(this.params.province!=null){
				 	this.citys=await this.getArea(this.params.province.level,this.params.province.adcode);
				}				
				if(this.params.city!=null){
					this.districts=await this.getArea(this.params.city.level,this.params.city.adcode);
				}
				if(this.params.district!=null){
					this.towns=await this.getArea('town',this.params.district.adcode);
				}
				if(this.params.town!=null){
					this.villages=await this.getArea('village',this.params.town.adcode);
				}
				
				
				
			}
		},
		methods: {
			
			// 地区初始化
			areaInit:function(){
				this.citys=[];
				this.districts=[];
				this.towns=[];
				this.villages=[];
			},
			
			// 获取省份列表
			getArea:async function(areaName,adcode){ 
				let res={}; 
				if (areaName == 'town' || areaName == 'village') {					
					res=await this.$api.yfk_api.getStreet({
						areaName: areaName,
						adcode: adcode
					})  
				}else {					
					res=await this.$api.yfk_api.getProvince({
						areaName: areaName,
						adcode: adcode
					})  
				}  
				if(res.data.ret==0){ 					
					return res.data.data;
				}
			},
			
			// 选择省份
			changeProvince:async function(e){ 
				
				let idx=e.detail.value; 
				let data=this.provinces[idx]; 
				this.params.province=data;  
				this.params.city.name='';
				this.params.district.name='';
				this.params.town.name='';
				this.params.village.name='';
				this.params.city.adcode='';
				this.params.district.adcode='';
				this.params.town.adcode='';
				this.params.village.adcode='';
				this.citys=[];
				this.districts=[];
				this.towns=[];
				this.villages=[]; 
				this.citys=await this.getArea(data.level,data.adcode) 
			},
			
			// 选择城市
			changeCity:async function(e){
				let idx=e.detail.value;
				let data=this.citys[idx];
				this.params.city=data;	 
				this.params.district.name='';
				this.params.town.name='';
				this.params.village.name='';
				this.params.district.adcode='';
				this.params.town.adcode='';
				this.params.village.adcode='';
				this.districts=[];
				this.towns=[];
				this.villages=[];
				this.districts=await this.getArea(data.level,data.adcode) 
			},
			
			// 选择区
			changeDistrict:async function(e){
				let idx=e.detail.value;
				let data=this.districts[idx];
				this.params.district=data; 
				this.params.town.name='';
				this.params.village.name='';
				this.params.town.adcode='';
				this.params.village.adcode='';
				this.towns=[];
				this.villages=[];
				this.towns=await this.getArea('town',data.adcode) 
			},
			
			// 选择镇/街道
			changeTown:async function(e){
				let idx=e.detail.value;
				let data=this.towns[idx];
				this.params.town=data;
				this.params.village.name='';
				this.params.village.adcode='';
				this.villages=await this.getArea('village',data.adcode) 
			},
			
			// 选择镇/街道
			changeVillage:async function(e){
				let idx=e.detail.value;
				let data=this.villages[idx];
				this.params.village=data; 
			}, 
			
			// 获取地区信息
			getUserAddressInfo:async function(){ 
				let update_userInfo=await this.$api.user_api.getUserInfo();  
				let res=await this.$api.user_api.getUserAddressInfo({
					province: update_userInfo.data.data.province,
					city: update_userInfo.data.data.city,
					district: update_userInfo.data.data.district,
					town:update_userInfo.data.data.town,
					village: update_userInfo.data.data.village,
				})  
				
				if(res.data.ret==0){  
					for(let key in res.data.data){ 						 
						if(res.data.data[key]==null){
							res.data.data[key]={};
							res.data.data[key]['name']='';
							res.data.data[key]['adcode']='';
							res.data.data[key]['level']='';
						} 					
					}  
					return res.data.data; 
				}
			},
			
			// 保存地区
			saveArea:async function () {  
			    let res=await this.$api.user_api.setUserInfo({
					realname: this.userInfo.realname,
					phone: this.userInfo.phone,
					province: this.params.province.adcode,
					city: this.params.city.adcode,
					district: this.params.district.adcode,
					town: this.params.town.adcode,
					village: this.params.village.adcode,
				})    
				console.log(res)
				if(res.data.ret==0){					 
					uni.showToast({
						icon:"success",
						title:"保存成功",
						mask:true,
						duration:1000,
						async success() {							 
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1000)
						}
					})
				}else{
					uni.showToast({
						icon:"error",
						title:"保存失败",
						mask:true,
						duration:1000,
						async success() {							 
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1000)
						}
					})
				}
				
			  
			},

		},
		computed: {

		},
		components: {

		}
	}
</script>

<style lang="less">
	.data-wrap {
		width: 96%;
		margin: 20rpx auto;
		background: #fff;
		border-radius: 10rpx;
		overflow: auto;

		.hd {
			padding: 15rpx 20rpx;
			border-bottom: 1rpx solid #ddd;

			text {
				padding-left: 30rpx;
				border-left: 10rpx solid #f00;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
	}

	.category-list {
		margin: 40rpx 0 10rpx;
	}

	.data-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		width: 94%;
		margin: 0 auto 20rpx;
		border-top: 1rpx dashed #ddd;

		.item {
			width: 22%;
			margin-top: 30rpx;
			font-size: 28rpx;
			text-align: center;

			.label {
				height: 90rpx;
				color: #999;
			}
		}
	}
</style>
